package com.ecube.maintenance.components.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;

import com.baidu.lbsapi.auth.LBSAuthManagerListener;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.navisdk.BNaviEngineManager;
import com.baidu.navisdk.BNaviPoint;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.comapi.routeplan.RoutePlanParams;
import com.ecube.maintenance.R;
import com.ecube.maintenance.components.application.CCBApplication;
import com.ecube.maintenance.pojos.AddressInfo;
import com.umeng.analytics.MobclickAgent;


/**
 * 两点之间导航
 */
public class BaiduMapNaviGuideActivity extends CustomFragmentActivity {

    AddressInfo from,to;
    Handler mHandler;

    public static Bundle getBundle(AddressInfo from,AddressInfo to){
        Bundle bundle = new Bundle();
        bundle.putSerializable("from", from);
        bundle.putSerializable("to", to);
        bundle.putSerializable("clazz",BaiduMapNaviGuideActivity.class);
        return bundle;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from = (AddressInfo) getIntent().getSerializableExtra("from");
        to = (AddressInfo) getIntent().getSerializableExtra("to");
        initBaiduNavi();
        mHandler = new Handler(){
            @Override
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                if ( msg.what == 0 ){
                    launchNavigator2();
                }
            }
        };
//        launchNavigator2();
    }

    /**
     * 指定导航起终点启动GPS导航.起终点可为多种类型坐标系的地理坐标。
     * 前置条件：导航引擎初始化成功
     */
    private void launchNavigator2(){
        //这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
        BNaviPoint startPoint = new BNaviPoint(from.getLon(),from.getLat(),
                from.getAddress(), BNaviPoint.CoordinateType.BD09_MC);
        BNaviPoint endPoint = new BNaviPoint(to.getLon(),to.getLat(),
                to.getAddress(), BNaviPoint.CoordinateType.BD09_MC);
        BaiduNaviManager.getInstance().launchNavigator(this,
                startPoint,                                      //起点（可指定坐标系）
                endPoint,                                        //终点（可指定坐标系）
                RoutePlanParams.NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME,       //算路方式
                true,                                            //真实导航
                BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //在离线策略
                new BaiduNaviManager.OnStartNavigationListener() {                //跳转监听

                    @Override
                    public void onJumpToNavigator(Bundle configParams) {
                        Intent intent = new Intent(
                                BaiduMapNaviGuideActivity.this,
                                BNavigatorActivity.class);
                        intent.putExtras(configParams);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onJumpToDownloader() {
                    }
                });
    }

    void initBaiduNavi(){
        BaiduNaviManager.getInstance().
                initEngine(this,getSdcardDir(),mNaviEngineInitListener,new LBSAuthManagerListener() {
                    @Override
                    public void onAuthResult(int i, String s) {

                    }
                });
    }
    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(
                Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }
    private boolean mIsEngineInitSuccess = false;
    private BNaviEngineManager.NaviEngineInitListener mNaviEngineInitListener = new BNaviEngineManager.NaviEngineInitListener() {
        public void engineInitSuccess() {
            //导航初始化是异步的，需要一小段时间，以这个标志来识别引擎是否初始化成功，为true时候才能发起导航
            mIsEngineInitSuccess = true;
            mHandler.sendEmptyMessage(0);
        }

        public void engineInitStart() {
        }

        public void engineInitFail() {
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
